package com.kjtpay.design_pattern.mediator;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.kjtpay.design_pattern.R;
import com.kjtpay.design_pattern.chain.ChainActivity;

public class MediatorActivity extends AppCompatActivity {

    public static void launch(Context context) {
        if (null != context) {
            Intent intent = new Intent();
            intent.setClass(context, MediatorActivity.class);
            if (!(context instanceof Activity)) {
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            }
            context.startActivity(intent);
        }
    }

    SecurityCouncil securityCouncil;
    USA usa;
    Iraq iraq;
    EditText editTextUSA;
    EditText editTextIraq;
    TextView textViewDeclare;
    String message;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mediator);
        setTitle("中介者模式");

        securityCouncil = new SecurityCouncil();
        usa = new USA(securityCouncil);
        iraq = new Iraq(securityCouncil);
        securityCouncil.usa = usa;
        securityCouncil.iraq = iraq;

        editTextUSA = findViewById(R.id.editTextUSA);
        editTextIraq = findViewById(R.id.editTextIraq);
        textViewDeclare = findViewById(R.id.textViewDeclare);

        message = "";
    }

    public void onClearClick(View view) {
        message = "";
        textViewDeclare.setText(message);
    }

    public void onUSAClick(View view) {
        // 美国声明；安理会转给伊拉克
        usa.declare(editTextUSA.getText().toString());
        // 展示伊拉克收到的信息
        message += iraq.showReceivedMessage();
        textViewDeclare.setText(message);
    }

    public void onIraqClick(View view) {
        // 伊拉克声明，安理会转给美国
        iraq.declare(editTextIraq.getText().toString());
        // 展示美国收到的信息
        message += usa.showReceivedMessage();
        textViewDeclare.setText(message);
    }
}